package com.online.teaching.module.teacher.homework.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.online.teaching.dao.AnswerMapper;
import com.online.teaching.dao.HomeworkMapper;
import com.online.teaching.dao.TeachesMapper;
import com.online.teaching.module.admin.blog.domain.vo.GetCommentListVO;
import com.online.teaching.module.teacher.homework.domain.dto.AnswerCorrentingDTO;
import com.online.teaching.module.teacher.homework.domain.dto.HomeWorkAddDTO;
import com.online.teaching.module.teacher.homework.domain.vo.GetAnswerListVO;
import com.online.teaching.module.teacher.homework.domain.vo.GetHomeWorkListVO;
import com.online.teaching.module.teacher.homework.service.TeacherHomeworkService;
import com.online.teaching.pojo.Answer;
import com.online.teaching.pojo.Homework;
import com.online.teaching.pojo.Teaches;
import com.online.teaching.utils.SmartBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.test.annotation.Rollback;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @ClassName : StudentBlogServiceImpl  //类名
 * @Description :   //描述
 * @Author : YG  //作者
 * @Date: 2020-11-04 13:21  //时间
 */
@Service
public class TeacherHomeworkServiceImpl implements TeacherHomeworkService {

    @Autowired
    private HomeworkMapper homeworkMapper;

    @Autowired
    private TeachesMapper teachesMapper;

    @Autowired
    private AnswerMapper answerMapper;

    @Override
    public int addHomework(HomeWorkAddDTO homeWorkAddDTO,Integer teaId) {
        //校验是不是这个老师教的
        Teaches teaches = teachesMapper.selectByPrimaryKey(homeWorkAddDTO.getTeachesId());
        if(teaches==null||!teaches.getTeaId().equals(teaId)){
            return 0;
        }

        Homework homework = SmartBeanUtil.copy(homeWorkAddDTO, Homework.class);
        return homeworkMapper.insert(homework);
    }

    /**
     * 不校验了。。。
     * @param homeworkId
     * @param teacherId
     * @return
     */
    @Rollback
    @Override
    public int deleteHomework(Integer homeworkId, Integer teacherId) {
        answerMapper.teacherDeleteHomework(homeworkId);
        int i = homeworkMapper.deleteHomework(homeworkId);
        return i;
    }

    @Override
    public PageInfo<GetHomeWorkListVO> getHomeWorkList(Integer teachesId,Integer teacherId, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        Teaches teaches = teachesMapper.selectByPrimaryKey(teachesId);
        if(teaches==null||!teaches.getTeaId().equals(teacherId)){
            return null;
        }
        List<Homework> homework = homeworkMapper.teacherGetHomeWorkList(teachesId);
        List<GetHomeWorkListVO> getHomeWorkListVOS = SmartBeanUtil.copyList(homework, GetHomeWorkListVO.class);
        return new PageInfo<>(getHomeWorkListVOS);
    }

    @Override
    public PageInfo<GetAnswerListVO> getAnswerList(Integer homeworkId, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<Answer> answerList = answerMapper.findAnswerList(homeworkId);
        List<GetAnswerListVO> getAnswerListVOS = SmartBeanUtil.copyList(answerList, GetAnswerListVO.class);
        return new PageInfo<>(getAnswerListVOS);
    }

    @Override
    public int correctingAnswer(AnswerCorrentingDTO answerCorrentingDTO,Integer teaId) {
        //校验有木有批改权限
        Integer homeworkId = answerCorrentingDTO.getHomeworkId();
        Integer teacherId = homeworkMapper.getTeacherId(teaId);
        if(teacherId==null||!teacherId.equals(teacherId)){
            return 0;
        }
        Answer copy = SmartBeanUtil.copy(answerCorrentingDTO, Answer.class);
        int i = answerMapper.updateByPrimaryKeySelective(copy);
        return i;
    }
}
